<?php 
namespace app\http;

use think\worker\Server;

class Worker extends Server
{
	protected $socket = 'websocket://0.0.0.0:2346';
 protected $name = 'thinkphp';
 protected $uid=10;
 protected $worker;
 public function onWorkerStart($worker){
	 echo '开始连接';
 
	 $this->worker=$worker;
 }
 
 public function onConnect($connection) {
 
	 $connection->uid=++$this->uid;
	  
 }
	public function onMessage($connection,$data)
	{
 
      $this->worker->uidConnections[$connection->uid]=$connection;
		//$connection->send(json_encode($data));
		 $connection->send('login success, your uid is ' . $connection->uid);
		 foreach($this->worker->connections as $conn)
		    {
					 
		        $conn->send("user[{$connection->uid}] said: $data");
		    }
	}
	
	public function onClose($connection) {
	  echo "闭关连接";
	}
}